import { fileURLToPath, URL } from 'node:url'

import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import tailwindcss from '@tailwindcss/vite'
import vueDevTools from 'vite-plugin-vue-devtools'
import Icons from 'unplugin-icons/vite'
import { type UserConfigExport, type ConfigEnv, loadEnv } from 'vite'

export default ({ mode }: ConfigEnv): UserConfigExport => {
  const { VITE_PORT, VITE_API_BASE_PATH, VITE_API_BASE_URL } = loadEnv(mode, process.cwd())
  return {
    plugins: [tailwindcss(), vue(), vueJsx(), vueDevTools(), Icons({ autoInstall: true })],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    server: {
      port: Number(VITE_PORT),
      host: '0.0.0.0',
      // 本地跨域代理 https://cn.vitejs.dev/config/server-options.html#server-proxy
      proxy: {
        '/api': {
          // 这里填写后端地址
          target: VITE_API_BASE_URL,
          changeOrigin: true
          // rewrite: path => path.replace(/^\/api/, "")
        }
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          api: 'modern-compiler'
        }
      }
    }
  }
}
